<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Unbelievable!</title>
</head>
<body>
    <!-- 为ECharts准备一个具备大小（宽高）的Dom -->
    <div id="main" style="width: 1200px;height:670px;"></div>
    <!-- <script src="shine.js"></script> -->
    <script src="echarts.js"></script>
    <script src="http://gallery.echartsjs.com/dep/echarts/map/js/province/beijing.js"></script>
</body>
</html>
<script>
    var geoCoordMap = {
        "东城区":[116.42,39.93],
        "西城区": [116.37,39.92],
        "朝阳区":[ 116.49, 40],
        "丰台区": [116.28,39.85],
        "石景山区": [116.22, 39.9],
        "海淀区":[116.21, 40.05],
        "门头沟区":[115.95, 40],
        "房山区" :[116, 39.64],
        "通州区": [116.7, 39.75],
        "顺义区" :[116.65,40.13],
        "昌平区" :[116.23, 40.22],
        "大兴区" :[116.33 ,39.73],
        "怀柔区":[ 116.63,40.52],
        "平谷区": [117.12, 40.13],
        "密云区":[116.83, 40.37],
        "延庆县" :[115.97, 40.45],

    };

    var data = [{name:'密云区' ,value:7},
      {name:'大兴区' ,value:9},
      {name:'房山区' ,value:42},
      {name:'石景山区' ,value:4},
      {name:'通州区' ,value:9},
      {name:'昌平区' ,value:1},
      {name:'怀柔区' ,value:2},
      {name:'东城区' ,value:12},
      {name:'丰台区' ,value:19},
      {name:'顺义区' ,value:4},
      {name:'朝阳区' ,value:24},
      {name:'海淀区' ,value:89},
      {name:'平谷区' ,value:3},
      {name:'西城区',value:16},
      {name:'门头沟区',value:3},
    ];
    var convertData = function(data) {
        var res = [];
        for (var i = 0; i < data.length; i++) {
        var geoCoord = geoCoordMap[data[i].name];//这是把指定城市的坐标提取出来呀
        if (geoCoord) {
            res.push({
                name: data[i].name,
                value: geoCoord.concat(data[i].value)
            });
        }
    }
    return res;//这是反复利用data么，感觉res[]还是data
};

var convertedData = [
convertData(data),
convertData(data.sort(function(a, b) {
    return b.value - a.value;
}).slice(0, 6))
];
data.sort(function(a,b){
   return    a.value-b.value;
})

var selectedItems = [];
var categoryData = [];
var barData = [];
 //   var maxBar = 30;
 var sum = 0;
 var count = data.length;
 for(var i=0;i<data.length;i++){
    categoryData.push(data[i].name);
    barData.push(data[i].value);
    sum+=data[i].value;
}
console.log(categoryData);
console.log(barData);
console.log(sum+"   "+count)
option = {
    backgroundColor: '#404a59',
    animation: true,
    animationDuration: 1000,
    animationEasing: 'cubicInOut',
    animationDurationUpdate: 1000,
    animationEasingUpdate: 'cubicInOut',
    title: [{
        text: '我的好友分布图',
        subtext: '你们都在哪里呢',
        left: 'center',
        textStyle: {
            color: '#fff'
        }
    }, {
        id: 'statistic',
        text: count ? '平均: ' +parseInt((sum / count).toFixed(4)) : '',
        right: 120,
        top: 40,
        width: 100,
        textStyle: {
            color: '#fff',
            fontSize: 16
        }//对bar的title的定义
    }],
    toolbox: {
        iconStyle: {
            normal: {
                borderColor: '#fff'
            },
            emphasis: {
                borderColor: '#b1e4ff'
            }
        },
        feature: {
            dataZoom: {},
            brush: {
                type: ['rect', 'polygon', 'clear']
            },
            saveAsImage: {
                show: true
            }
        }
    },
    brush: {
        outOfBrush: {
            color: '#abc'
        },
        brushStyle: {
            borderWidth: 2,
            color: 'rgba(0,0,0,0.2)',
            borderColor: 'rgba(0,0,0,0.5)',
        },
        seriesIndex: [0, 1],
        throttleType: 'debounce',
        throttleDelay: 300,
        geoIndex: 0
    },
    geo: {
        map: '北京',
        left: '10',
        right: '35%',
        // center: [117.98561551896913, 31.205000490896193],
        zoom: 1.5,
        label: {
            emphasis: {
                show: false
            }
        },
        roam: true,
        itemStyle: {
            normal: {
                areaColor: '#323c48',
                borderColor: '#111'
            },
            emphasis: {
                areaColor: '#2a333d'
            }
        }
    },
    tooltip: {
        trigger: 'item'
    },
    grid: {
        right: 40,
        top: 100,
        bottom: 40,
        width: '30%'
    },
    xAxis: {
        type: 'value',
        scale: true,
        position: 'top',
        boundaryGap: false,
        splitLine: {
            show: false
        },
        axisLine: {
            show: false
        },
        axisTick: {
            show: false
        },
        axisLabel: {
            margin: 2,
            textStyle: {
                color: '#aaa'
            }
        },
    },
    yAxis: {
        type: 'category',
        //  name: 'TOP 20',
        nameGap: 16,
        axisLine: {
            show: true,
            lineStyle: {
                color: '#ddd'
            }
        },
        axisTick: {
            show: false,
            lineStyle: {
                color: '#ddd'
            }
        },
        axisLabel: {
            interval: 0,
            textStyle: {
                color: '#ddd'
            }
        },
        data: categoryData
    },
    series: [{
        // name: 'pm2.5',
        type: 'scatter',
        coordinateSystem: 'geo',
        data: convertedData[0],
        symbolSize: function(val) {
            return Math.max(val[2] / 10, 8);
        },
        label: {
            normal: {
                formatter: '{b}',
                position: 'right',
                show: false
            },
            emphasis: {
                show: true
            }
        },
        itemStyle: {
            normal: {
                color: '#ddb926',
                position: 'right',
                show: true
            }
        }
    }, {
        //  name: 'Top 5',
        type: 'effectScatter',
        coordinateSystem: 'geo',
        data: convertedData[0],
        symbolSize: function(val) {
            return Math.max(val[2] / 10, 8);
        },
        showEffectOn: 'emphasis',
        rippleEffect: {
            brushType: 'stroke'
        },
        hoverAnimation: true,
        label: {
            normal: {
                formatter: '{b}',
                position: 'right',
                show: true
            }
        },
        itemStyle: {
            normal: {
                color: '#f4e925',
                shadowBlur: 10,
                shadowColor: '#333'
            }
        },
        zlevel: 1
    }, {
        id: 'bar',
        zlevel: 2,
        type: 'bar',
        symbol: 'none',
        itemStyle: {
            normal: {
                color: '#ddb926'
            }
        },

        data: data
    }]
};


var myChart= echarts.init(document.getElementById('main'));
myChart.setOption(option);
myChart.on('brushselected', renderBrushed);

function renderBrushed(params) {
    var mainSeries = params.batch[0].selected[0];

    var selectedItems = [];
    var categoryData = [];
    var barData = [];
    var maxBar = 30;
    var sum = 0;
    var count = 0;

    for (var i = 0; i < mainSeries.dataIndex.length; i++) {
        var rawIndex = mainSeries.dataIndex[i];
        var dataItem = convertedData[0][rawIndex];
        var pmValue = dataItem.value[2];

        sum += pmValue;
        count++;

        selectedItems.push(dataItem);
    }

       selectedItems.sort(function (a, b) {
        //   return b.value[2] - a.value[2];
        return a.value-b.value;
       });

    for (var i = 0; i < Math.min(selectedItems.length, maxBar); i++) {
        categoryData.push(selectedItems[i].name);
        barData.push(selectedItems[i].value[2]);
    }

    this.setOption({
        yAxis: {
            data: categoryData
        },
        xAxis: {
            axisLabel: {
                show: !!count
            }
        },
        title: {
            id: 'statistic',
            text: count ? '平均: ' + (sum / count).toFixed(4) : ''
        },
        series: {
            id: 'bar',
            //        sort:'descending',
            data: barData
        }
    });
}

</script>